package cn.tanghom.baiding.service.system.service;

import cn.tanghom.baiding.api.system.model.Resources;
import cn.tanghom.baiding.api.system.service.ResourcesService;
import cn.tanghom.baiding.core.service.impl.BaseServiceImpl;
import cn.tanghom.baiding.core.tree.SimpleJsonTree;
import cn.tanghom.baiding.core.utils.JsonUtil;
import cn.tanghom.baiding.service.system.dao.ResourcesDao;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 菜单资源管理逻辑处理层
 *
 * @author tanghom<tanghom@qq.com> on 2016/5/16
 */
@Service("rsourcesService")
public class ResourcesServiceImpl extends BaseServiceImpl<Resources> implements ResourcesService {
    @Resource
    private ResourcesDao resourcesDao;

    @Override
    public List<Resources> selectListByParams(Integer status) {
        return resourcesDao.selectListByParams(status);
    }

    @Override
    public List<Resources> findUserResourcess(Long userId) {
        return resourcesDao.findUserResourcess(userId);
    }

    @Override
    public List<Resources> findRoleResourcess(Long roleId) {
        return resourcesDao.findRoleResourcess(roleId);
    }

    @Override
    public String findListToSimpleJson(Integer status) {
        Resources resources = new Resources();
        resources.setStatus(status);
        List<Resources> resourcesList = resourcesDao.selectTByT(resources);
        List<SimpleJsonTree> simpleJsonTreeList = new ArrayList<SimpleJsonTree>();
        if (null != resourcesList) {
            for (int i = 0; i < resourcesList.size(); i++) {
                Resources res = resourcesList.get(i);
                Integer id = res.getId()!=null? res.getId().intValue():0;
                Integer pId = res.getParentId()!=null? res.getParentId().intValue():0;
                SimpleJsonTree simpleJsonTree = new SimpleJsonTree(id, pId, res.getName());
                simpleJsonTreeList.add(simpleJsonTree);
            }
        }
        return JsonUtil.toJSONString(simpleJsonTreeList);
    }
}
